<template>
    <div id="PlaceOfSale">
        <div class="header-container">
            <h1>销售地点管理内容</h1>
            <el-input placeholder="请输入ID" prefix-icon="el-icon-search" v-model="sale_id" size="medium"
                class="header-input" />
            <el-input placeholder="请输入药店名称" prefix-icon="el-icon-search" v-model="sale_name" size="medium"
                class="header-input" />
            <el-select v-model="city_name" placeholder="请选择" size="medium" class="header-select">
                <el-option v-for="item in cityList" :key="item.city_id" :label="item.city_name"
                    :value="item.city_name" />
            </el-select>
            <el-button id="search" type="primary" icon="el-icon-search" @click="search()" size="medium"
                class="header-btn">搜索</el-button>
                 <el-button id="search" type="primary" icon="el-icon-search" @click="resetSearch()" size="medium"
                class="header-btn">重置</el-button>
            <el-button type="success" class="add-button header-btn" @click="addSale()" size="medium">新增药店</el-button>
        </div>

        <el-table :data="pm.list" border style="width: 100%">

            <el-table-column fixed prop="sale_id" label="ID" width="200">
            </el-table-column>

            <el-table-column prop="sale_name" label="药店名称" width="350">
            </el-table-column>

            <el-table-column prop="sale_phone" label="药店号码" width="350">
            </el-table-column>

            <el-table-column prop="city.city_name" label="所属城市" width="250">
            </el-table-column>

            <el-table-column fixed="right" label="操作" width="150">
                <template slot-scope="scope">
                    <el-button type="primary" @click="showMapDialog(scope.row)">查看地图</el-button>
                    <el-button type="primary" icon="el-icon-edit" @click="updateSale(scope.row)">
                    </el-button>
                    <el-button type="danger" icon="el-icon-delete" @click="deleteSale(scope.row.sale_id)">
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination background layout="total, sizes, prev, pager, next, jumper" :total="pm.totalRecords"
            :page-size="pm.pageSize" :current-page="pm.currentPageNum" :page-sizes="[5, 10, 20, 50]"
            @size-change="sizeChange" @current-change="pageChange" style="margin-top: 20px; text-align: center;">
        </el-pagination>



        <el-dialog title="编辑药店信息" :visible.sync="updateFormvisible" :modal="false" width="350px">
            <el-form :model="sale" :label-width="'80px'">

                <el-form-item label="药店名称">
                    <el-input v-model="sale.sale_name"></el-input>
                </el-form-item>

                <el-form-item label="药店号码">
                    <el-input v-model="sale.sale_phone"></el-input>
                </el-form-item>

                <el-form-item label="所属城市">
                    <el-select v-model="sale.city.city_id" placeholder="请选择所属城市">
                        <el-option v-for="item in cityList" :key="item.city_id" :label="item.city_name"
                            :value="item.city_id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="纬度">
                    <el-input v-model="sale.latitude"></el-input>
                </el-form-item>

                <el-form-item label="经度">
                    <el-input v-model="sale.longitude"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelUpdate()">取 消</el-button>
                <el-button type="primary" @click="submmitUpdate()">提 交</el-button>
            </div>
        </el-dialog>


        <el-dialog title="新增药店" :visible.sync="addFormvisible" :modal="false" width="350px">
            <el-form :model="sale" :label-width="'80px'">

                <el-form-item label="药店名称">
                    <el-input v-model="sale.sale_name"></el-input>
                </el-form-item>

                <el-form-item label="药店号码">
                    <el-input v-model="sale.sale_phone"></el-input>
                </el-form-item>

                <el-form-item label="所属城市">
                    <el-select v-model="sale.city.city_id" placeholder="请选择所属城市">
                        <el-option v-for="item in cityList" :key="item.city_id" :label="item.city_name"
                            :value="item.city_id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="纬度">
                    <el-input v-model="sale.latitude"></el-input>
                </el-form-item>

                <el-form-item label="经度">
                    <el-input v-model="sale.longitude"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelAdd()">取 消</el-button>
                <el-button type="primary" @click="submmitAdd()">提 交</el-button>
            </div>
        </el-dialog>

        <el-dialog 
          title="地图定位" 
          :visible.sync="mapDialogVisible" 
          width="800px"
          :modal="false"
          @opened="initMap"
        >
          <div id="mapContainer" style="width: 100%; height: 400px;"></div>
          <span slot="footer" class="dialog-footer">
            <el-button @click="mapDialogVisible = false">关闭</el-button>
          </span>
        </el-dialog>
    </div>
</template>

<script>
/* eslint-disable no-undef */
import axios from 'axios';

export default {
    name: "PlaceOfSale",
    data() {
        return {
            pm: {
                currentPageNum: 1,
                totalRecords: 0,
                pageSize: 5,
                totalPageNum: 0,
                startIndex: 0,
                prePageNum: 0,
                nextPageNum: 0,
                list: [],
                startPage: 0,
                endPage: 0
            },
            sale: {
                sale_id: null,
                sale_name: "",
                sale_phone: "",
                latitude: null,
                longitude: null,
                city: {
                    city_id: null,
                    city_name: ""
                }
            },
            cityList: [],
            updateFormvisible: false,
            addFormvisible: false,
            sale_id: null,
            sale_name: "",
            city_name: "",
            mapDialogVisible: false,
            currentLocation: {
                latitude: null,
                longitude: null,
                name: ''
            }
        }
    },
    mounted() {
        this.getSalePage(1);
        this.getCityList();
    },
    methods: {
        getSalePage(e) {
            axios.get("http://localhost:8080/getSalePage", { params: { num: e, size: this.pm.pageSize } }).then(res => {
                this.pm = res.data
            })
        },
        sizeChange(size) {
            this.pm.pageSize = size;
            this.getSalePage(1);
        },
        pageChange(num) {
            this.getSalePage(num);
        },
        deleteSale(sale_id) {
            if (confirm("确定删除？")) {
                axios.delete("http://localhost:8080/deleteSale", { params: { sale_id } }).then(res => {
                    alert(res.data)
                    this.getSalePage(1);
                })
            }
        },
        getCityList() {
            axios.get("http://localhost:8080/getCityList").then(res => {
                this.cityList = res.data;
            })
        },
        updateSale(sl) {
            this.getCityList();
            this.sale = sl;
            this.updateFormvisible = true
        },
        submmitUpdate() {
            axios.put("http://localhost:8080/updateSale", this.sale).then(res => {
                alert(res.data);
                this.getSalePage(this.pm.currentPageNum);
                this.updateFormvisible = false;
                this.resetSale();
            })
        },
        resetSale() {
            this.sale = {
                sale_id: null,
                sale_name: "",
                sale_phone: "",
                latitude: null,
                longitude: null,
                city: {
                    city_id: null,
                    city_name: ""
                }
            }
        },
        cancelUpdate() {
            this.updateFormvisible = false;
            this.resetSale();
        },
        addSale() {
            this.getCityList();
            this.addFormvisible = true;
        },
        submmitAdd() {
            axios.post("http://localhost:8080/addSale", this.sale).then(res => {
                alert(res.data);
                this.getSalePage(this.pm.totalPageNum);
                this.resetSale();
                this.addFormvisible = false;
            })
        },
        cancelAdd() {
            this.resetSale();
            this.addFormvisible = false;
        },
        search(){
            axios.get("http://localhost:8080/getSaleByConditions",{params:{sale_id:this.sale_id,sale_name:this.sale_name,city_name:this.city_name}}).then(res=>{
                this.pm.list=res.data;
            }
            )
        },
        resetConditions(){
            this.sale_id=null;
            this.sale_name="";
            this.city_name="";
        },
        resetSearch(){
            this.resetConditions();
            this.getSalePage(1);
        },
        showMapDialog(row) {
            this.currentLocation = {
                latitude: row.latitude,
                longitude: row.longitude,
                name: row.sale_name || '未知'
            }
            this.mapDialogVisible = true
        },
        initMap() {
            this.$nextTick(() => {
                const { latitude, longitude, name } = this.currentLocation
                if (!latitude || !longitude) {
                    this.$message.warning('该药店暂无坐标信息');
                    return;
                }
                

                const map = new BMap.Map('mapContainer')
                const point = new BMap.Point(longitude, latitude)
                map.centerAndZoom(point, 16)
                map.addControl(new BMap.NavigationControl())
                map.enableScrollWheelZoom(true)
                
                const marker = new BMap.Marker(point)
                map.addOverlay(marker)
                const label = new BMap.Label(name, { offset: new BMap.Size(20, -10) })
                marker.setLabel(label)
                map.panTo(point)
            })
        }
    }
}
</script>

<style>
#PlaceOfSale {
    width: 100%;
    height: 100%;
}
.header-container {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
}

.header-container h1 {
  font-size: 20px;
  margin-right: 24px;
  white-space: nowrap;
}

.header-input,
.header-select {
  width: 200px;
  height: 40px;
}

.header-btn {
  height: 40px;
  min-width: 90px;
  padding: 0 16px;
  font-size: 16px;
}
</style>